﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using IServices;
using GestionLinqLibData;
using IRepositories;
using LinqRepositories;
using Services.Validation;


namespace Services
{
    public class NoteService : INoteService
    {
        private IValidationDictionary _validationDictionary;

        private INoteRepository _repository;

        public NoteService(IValidationDictionary validationDictionary)
            : this(validationDictionary, new LinqNoteRepository())
        { }
        public NoteService(IValidationDictionary validationDictionary, INoteRepository repository)
        {
            _validationDictionary = validationDictionary;
            _repository = repository;
        }

        protected bool ValidateNotes(IEnumerable<note> notesToValidate)
        {
            foreach (var note in notesToValidate)
            {
                if (note.valeur > 20 || note.valeur < 0)
                    _validationDictionary.AddError("Note Invalide", "Note Invalide");
            }
            return _validationDictionary.IsValid;
        }

        #region INoteService Membres

        public IEnumerable<note> ConsulterNote(int id_matiere, int idexam)
        {
            return _repository.ConsulterNote(id_matiere, idexam);
        }

        public bool AddNote(IEnumerable<note> listeNote)
        {
            if (!ValidateNotes(listeNote))
                return false;

            try
            {
                _repository.AddNote(listeNote);
                
            }
            catch 
            {

                return false;
            }

            return true;

        }

        

        public IEnumerable<note> AddEntries(int id_matiere)
        {
           return _repository.AddEntries(id_matiere);
        }

        #endregion
    }
}